import {
    Dispatch
} from 'redux';

import Ajax from './../../http';
import * as types from './../types/setting';

export default {
    //获取推送状态
    [types.getNotifyStatus]() {
        return async (dispatch: Dispatch) => {
            try {
                const status = (
                    await Ajax.get(
                        `notification/status`
                    )
                )
                dispatch({
                    data: status,
                    type: types.getNotifyStatus
                })
            }
            catch (e) {
                return Promise.reject(e)
            }
        }
    },
    //修改推送状态
    [types.setNotifyStatus](type: string, status: number) {
        return async (dispatch: Dispatch) => {
            const _url = `notification/${type}/${status}`;
            try {
                await Ajax.put(_url)
                dispatch({
                    type: types.setNotifyStatus,
                    data: {
                        type: type,
                        status: status
                    }
                })
            }
            catch (e) {
                return Promise.reject(e)
            }
        }
    }
}